/**
 * 
 */
package com.tiduyun.data.exchange.util;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;

/**
 * @author wuzheng@tiduyun.com
 *
 */
public class HttpUrlUtils {

    public static String buildUrl(String baseUrl, String path, String queryString) {
        baseUrl = StringUtils.trim(baseUrl);
        path = StringUtils.trim(path);
        queryString = StringUtils.trim(queryString);
        String url = simpleJoinString(baseUrl, path, "/");
        url = simpleJoinString(url, queryString, "?");
        return url;
    }

    public static String buildUrl(String baseUrl, String path) {
        return buildUrl(baseUrl, path, null);
    }

    public static String appendQueryString(String baseUrl, String queryString) {
        baseUrl = StringUtils.trim(baseUrl);
        queryString = StringUtils.trim(queryString);
        int urlSeparatorIndex = StringUtils.indexOf(baseUrl, "?");
        if (urlSeparatorIndex >= 0) {
            if (urlSeparatorIndex == (baseUrl.length() - 1)) {
                return StringUtils.join(baseUrl, queryString);
            } else {
                return StringUtils.join(baseUrl, "&", queryString);
            }
        } else {
            return StringUtils.join(baseUrl, "?", queryString);
        }
    }

    private static String simpleJoinString(String left, String right, String joiner) {
        if (StringUtils.endsWith(left, joiner) || StringUtils.startsWith(right, joiner)) {
            return StringUtils.join(left, right);
        }

        if (left == null) {
            return right;
        } else if (right == null) {
            return left;
        } else {
            return new StringBuilder().append(left).append(joiner).append(right).toString();
        }
    }

    public static String replacePathVariable(String urlPath, List<? extends NameValuePair> params) {
        return replacePathVariable(urlPath, params, false);
    }

    public static String replacePathVariable(String urlPath, List<? extends NameValuePair> params, boolean removeReplacedParam) {
        if (CollectionUtils.isNotEmpty(params) && StringUtils.contains(urlPath, "{") && StringUtils.contains(urlPath, "}")) {
            List<String> searchList = new ArrayList<>();
            List<String> replacementList = new ArrayList<>();
            String paramReplaceHolder = null;
            NameValuePair param = null;
            for (int i = 0; i < params.size();) {
                param = params.get(i);
                paramReplaceHolder = StringUtils.join("{", param.getName(), "}");

                if (removeReplacedParam && StringUtils.contains(urlPath, paramReplaceHolder)) {
                    searchList.add(paramReplaceHolder);
                    replacementList.add(param.getValue());

                    params.remove(i);
                } else {
                    ++i;
                }
            }
            return StringUtils.replaceEach(urlPath, searchList.toArray(new String[0]), replacementList.toArray(new String[0]));
        }
        return urlPath;
    }

    public static URI buildUri(String url, List<NameValuePair> params) throws URISyntaxException {
        if (CollectionUtils.isNotEmpty(params)) {
            URIBuilder uriBuilder = new URIBuilder(url);
            uriBuilder.setParameters(params);
            return uriBuilder.build();
        } else {
            return URI.create(url);
        }
    }
    
    public static URI buildUri(URI uri, List<NameValuePair> params) throws URISyntaxException {
        if (CollectionUtils.isNotEmpty(params)) {
            URIBuilder uriBuilder = new URIBuilder(uri);
            uriBuilder.setParameters(params);
            return uriBuilder.build();
        } else {
            return uri;
        }
    }
}
